//
//  WTFBaseTableVc.m
//  Courses
//
//  Created by xxx on 15/1/3.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "WTFBaseTableVc.h"
#import "EGORefreshTableHeaderView.h"

@interface WTFBaseTableVc() <EGORefreshTableHeaderDelegate> //<UITableViewDataSource, UITableViewDelegate>

@end

@implementation WTFBaseTableVc
{
//    EGORefreshTableHeaderView *_refreshHeaderView;
    NSTimer *_timer;
}

- (id)init
{
    if (self = [super init])
    {
        self.showsTabBar = YES;
        self.enableRefresh = YES;
        self.enableLoadMore = YES;
        self.pageNum = 1;
        self.isLoading = NO;
        
        if (!_items)
        {
            _items = [[NSMutableArray alloc] init];
        }
        
//        CGRect tableRect = AutoRectMake(0, DEFAULT_NAV_BAR_HEIGHT, DEFAULT_WIDTH, DEFAULT_TABLE_VIEW_HEIGHT, WTFAutoLayoutTypePic);
//        if (!_showsTabBar)
//        {
//            tableRect.size.height += DEFAULT_TABBAR_HEIGHT;
//        }
//        _tableView = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStylePlain];
//        _tableView.dataSource = self;
//        _tableView.delegate = self;
//        [self.view addSubview:_tableView];
        
//        __weak WTFBaseTableVc *weakSelf = self;
//
//        
        if (_enableRefresh)
        {
//            EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - 44, self.view.frame.size.width, 44)];
//            view.delegate = self;
//            [self.tableView addSubview:view];
//            _refreshHeaderView = view;
//            
//            [_refreshHeaderView refreshLastUpdatedDate];
        }
//
//        if (_enableLoadMore)
//        {
//            // setup infinite scrolling
//            [self.tableView addInfiniteScrollingWithActionHandler:^{
//                [weakSelf insertRowAtBottom];
//            }];
//        }
    }
    return self;
}

//- (void)insertRowAtTop {
//    __weak WTFBaseTableVc *weakSelf = self;
//    
//    int64_t delayInSeconds = 2.0;
//    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//        [weakSelf.tableView beginUpdates];
//        [weakSelf.items insertObject:[NSDate date] atIndex:0];
//        [weakSelf.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
//        [weakSelf.tableView endUpdates];
//        
//        [weakSelf.tableView.pullToRefreshView stopAnimating];
//    });
//}
//
//
//- (void)insertRowAtBottom {
//    __weak WTFBaseTableVc *weakSelf = self;
//    
//    int64_t delayInSeconds = 2.0;
//    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//        [weakSelf.tableView beginUpdates];
//        [weakSelf.items addObject:[weakSelf.items.lastObject dateByAddingTimeInterval:-90]];
//        [weakSelf.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:weakSelf.items.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
//        [weakSelf.tableView endUpdates];
//        
//        [weakSelf.tableView.infiniteScrollingView stopAnimating];
//    });
//}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [WTFColors defaultBarColor];
    
    UIColor * cc = [UIColor whiteColor];
    NSDictionary * dict = [NSDictionary dictionaryWithObject:cc forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    NSLog(@"Navigating: %@", [self class]);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}

- (void)reloadRequest
{
    [self.items removeAllObjects];
    [self loadRequest];
}

- (void)loadRequest
{
    if (self.isLoading) return;
    
    [self performSelectorInBackground:@selector(loadInBg) withObject:nil];
}

- (void)loadInBg
{
    _isLoading = YES;
    NSArray *fetchedItems = [self fetchedItems];
//    if (_pageNum == 1) [self.items removeAllObjects];
    if (fetchedItems.count) {
        [self.items addObjectsFromArray:fetchedItems];
    }
    [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:NO];
}

- (void)refresh{
    self.isLoading = NO;
    [self.tableView reloadData];
    self.tableView.contentOffset = CGPointMake(0, 0);//让tableview每次刷新数据后都从第一行显示
};

- (BOOL)isLoading
{
    return _isRefreshing;
}

- (NSArray *)fetchedItems
{
    return nil;
}


//#pragma mark -
//#pragma mark Data Source Loading / Reloading Methods
//
//- (void)reloadTableViewDataSource{
//    
//    //  should be calling your tableviews data source model to reload
//    //  put here just for demo
//    _isRefreshing = YES;
//    
//}
//
//- (void)doneLoadingTableViewData{
//    
//    //  model should call this when its done loading
//    _isRefreshing = NO;
//    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
//    
//}
//
//#pragma mark -
//#pragma mark UIScrollViewDelegate Methods
//
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    
//    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
//    
//}
//
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    
//    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
//    
//}
//
//#pragma mark -
//#pragma mark EGORefreshTableHeaderDelegate Methods
//
//- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
//    
//    [self reloadTableViewDataSource];
//    [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
//    
//}
//
//- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
//    
//    return _isRefreshing; // should return if data source model is reloading
//    
//}
//
//- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
//    
//    return [NSDate date]; // should return date data source was last changed
//    
//}
//控制界面旋转
- (BOOL) shouldAutorotate{  //应用支持横竖屏 在这里设置为NO ，所有的子Controller默认都是不支持横屏
    return NO;
}
//检测网络状态
-(void)showNetStatus{
    if ([PXHGetNetworkStatus isHaveNetWork]) {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"无网络访问请链接网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
@end
